package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApAuthorMapper;
import com.heima.article.service.ApAuthorService;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ApAuthorServiceImpl extends ServiceImpl<ApAuthorMapper, ApAuthor> implements ApAuthorService {

    /**
     * 文章作者添加用户
     * @param apAuthor
     * @return
     */
    @Transactional
    @Override
    public ResponseResult seveApAuthor(ApAuthor apAuthor) {

        //查询用户是否已经存在
        LambdaQueryWrapper<ApAuthor> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ApAuthor::getUserId, apAuthor.getUserId());
        ApAuthor one = getOne(wrapper);
        if (null != one) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }

        //储存用户信息
        boolean save = save(apAuthor);

        //获取作者id
        ApAuthor author = getOne(wrapper);

        //判断是否保存成功
        if (save) {
            //保存成功返回作者id
            return ResponseResult.okResult(author.getId());
        }

        //保存失败返回错误
        return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
    }
}
